/*
 * $Id: ProgrammeAccreditationAction.java 3 2011-12-26 07:48:43Z forwardly@hotmail.com $
 * 
 * Copyright (c) 2001-2008 Accentrix Company Limited. All Rights Reserved.
 * 
 * Accentrix Company Limited. ("Accentrix") retains copyright on all text, source
 * and binary code contained in this software and documentation. Accentrix grants
 * Licensee a limited license to use this software, provided that this copyright
 * notice and license appear on all copies of the software. The software source
 * code is provided for reference purposes only and may not be copied, modified 
 * or distributed.
 * 
 * THIS SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS," WITHOUT ANY WARRANTY OF
 * ANY KIND UNLESS A SEPARATE WARRANTIES IS PURCHASED FROM ACCENTRIX AND REMAINS
 * VALID.  ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. ACCENTRIX SHALL NOT BE LIABLE
 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING OR MODIFYING THE
 * SOFTWARE OR ITS DERIVATIVES.
 * 
 * IN NO EVENT WILL ACCENTRIX BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
 * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE
 * USE OF OR INABILITY TO USE SOFTWARE, EVEN IF ACCENTRIX HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */
package com.lightsaber.trade.as.web.programme;

import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.lightsaber.trade.as.entity.programme.HkcaavqValidation;
import com.lightsaber.trade.as.entity.programme.InternalValidation;
import com.lightsaber.trade.as.entity.programme.Programme;
import com.lightsaber.trade.as.entity.programme.ProgrammeCode;
import com.lightsaber.trade.as.service.param.ParamService;
import com.lightsaber.trade.as.service.programme.HkcaavqValidationManager;
import com.lightsaber.trade.as.service.programme.InternalValidationManager;
import com.lightsaber.trade.as.service.programme.ProgrammeCodeManager;
import com.lightsaber.trade.as.service.programme.ProgrammeManager;
import com.lightsaber.trade.core.web.BaseAction;

@Namespace("/programme")
@Results({ @Result(name = "HKCAAVQ-INPUT", location = "hkcaavq-input.jsp"),
        @Result(name = "INTERNAL-INPUT", location = "internal-input.jsp") })
public class ProgrammeAccreditationAction extends BaseAction {

    private static final long serialVersionUID = 161519400935008339L;

    @Autowired
    private transient ParamService paramService;
    @Autowired
    private ProgrammeManager programmeManager;
    @Autowired
    ProgrammeCodeManager programmeCodeManager;
    @Autowired
    private InternalValidationManager internalValidationManager;
    @Autowired
    private HkcaavqValidationManager hkcaavqValidationManager;

    private Programme programme;
    private String programmeId;

    private List<InternalValidation> internalValidations;
    private List<HkcaavqValidation> hkcaavqValidations;

    // input page parameters
    private String code;

    private InternalValidation internalValidation;

    private HkcaavqValidation hkcaavqValidation;

    @Override
    public String execute() {
        return list();
    }

    public String list() {
        programme = programmeManager.get(programmeId);
        code = programme.getCode();

        ProgrammeCode programmeCode = programmeCodeManager.getByCode(programme.getCode());

        hkcaavqValidations = programmeCode.getHkcaavqValidations();
        internalValidations = programmeCode.getInternalValidations();

        return SUCCESS;
    }

    public String hkcaavqInput() {
        if (hkcaavqValidation != null && StringUtils.isNotBlank(hkcaavqValidation.getId())) {
            hkcaavqValidation = hkcaavqValidationManager.get(hkcaavqValidation.getId());
        }

        return "HKCAAVQ-INPUT";
    }

    public String internalInput() {
        if (internalValidation != null && StringUtils.isNotBlank(internalValidation.getId())) {
            internalValidation = internalValidationManager.get(internalValidation.getId());
        }

        return "INTERNAL-INPUT";
    }

    public String saveHkcaavq() {
        try {
            hkcaavqValidationManager.save(code, hkcaavqValidation);
            addActionMessage(getText(PROCESSED_SUCCESS));
        } catch (Exception e) {
            addActionError(e.getMessage());
        }
        return list();
    }

    public String saveInternal() throws Exception {
        internalValidationManager.save(code, internalValidation);
        addActionMessage(getText(PROCESSED_SUCCESS));
        return list();
    }

    public String deleteHkcaavq() throws Exception {
        hkcaavqValidationManager.delete(hkcaavqValidation.getId());
        addActionMessage(getText(PROCESSED_SUCCESS));
        return list();
    }

    public String deleteInternal() throws Exception {
        internalValidationManager.delete(internalValidation.getId());
        addActionMessage(getText(PROCESSED_SUCCESS));
        return list();
    }

    public String save() throws Exception {
        return INPUT;
    }

    public String getProgrammeId() {
        return programmeId;
    }

    public void setProgrammeId(final String programmeId) {
        this.programmeId = programmeId;
    }

    public Programme getProgramme() {
        return programme;
    }

    public void setProgramme(final Programme programme) {
        this.programme = programme;
    }

    public List<InternalValidation> getInternalValidations() {
        return internalValidations;
    }

    public InternalValidation getInternalValidation() {
        return internalValidation;
    }

    public void setInternalValidation(final InternalValidation internalValidation) {
        this.internalValidation = internalValidation;
    }

    public HkcaavqValidation getHkcaavqValidation() {
        return hkcaavqValidation;
    }

    public void setHkcaavqValidation(final HkcaavqValidation hkcaavqValidation) {
        this.hkcaavqValidation = hkcaavqValidation;
    }

    public ParamService getParamService() {
        return paramService;
    }

    public void setParamService(final ParamService paramService) {
        this.paramService = paramService;
    }

    public String getCode() {
        return code;
    }

    public void setCode(final String code) {
        this.code = code;
    }

    public List<HkcaavqValidation> getHkcaavqValidations() {
        return hkcaavqValidations;
    }

    public void setHkcaavqValidations(final List<HkcaavqValidation> hkcaavqValidations) {
        this.hkcaavqValidations = hkcaavqValidations;
    }

    public void setInternalValidations(final List<InternalValidation> internalValidations) {
        this.internalValidations = internalValidations;
    }

}
